﻿using System;
using System.Windows.Forms;
using Library;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;

namespace QLCF.Forms
{
    public partial class frmRestoreDB : Form
    {
        const string m_database = "QLCF";
        public frmRestoreDB()
        {
            InitializeComponent();
        }

        private void frmBackupDB_Load(object sender, EventArgs e)
        {
            try
            {
                //check database exists                
                Server SqlServer = new Server(Utils.Setting.ServerName);
                ServerConnection SqlServerConnection = SqlServer.ConnectionContext;
                SqlServerConnection.LoginSecure = true;
                SqlServerConnection.DatabaseName = "master";

                if (SqlServer.Databases[m_database] != null)
                {

                    Database db = SqlServer.Databases[m_database];
                    lblCreateDate.Text = db.CreateDate.ToString("dd/MM/yyyy HH:mm:ss");
                    lblLastBackupDate.Text = db.LastBackupDate.ToString("dd/MM/yyyy HH:mm:ss");
                    lblPrimaryFilePath.Text = db.PrimaryFilePath;

                    if (!string.IsNullOrEmpty(clsConfigurationSettings.GetSetting("PathFileBackup")))
                        txtPath.Text = clsConfigurationSettings.GetSetting("PathFileBackup");
                }
                else
                {
                    MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu");
                    this.Close();
                }

            }
            catch
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu");
                this.Close();
            }
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Phục hồi dữ liệu sẽ xóa toàn bộ dữ liệu hiện tại!\n Bạn có chắc chắn không?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                prbProgress.Minimum = 0;
                prbProgress.Maximum = 100;
                prbProgress.Value = 0;
                prbProgress.Visible = true;

                try
                {
                    string destinationPath = txtPath.Text;
                    clsRestoreHelper restoreDB = new clsRestoreHelper();
                    restoreDB.Complete += new EventHandler<ServerMessageEventArgs>(backup_Complete);
                    restoreDB.PercentComplete += new EventHandler<PercentCompleteEventArgs>(backup_PercentComplete);
                    restoreDB.RestoreDatabase(m_database, destinationPath, Application.StartupPath + "\\data\\" , Application.StartupPath + "\\data\\");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi phục hồi cơ sở dữ liệu", "Thông báo");
                }
            }
        }

        public void backup_Complete(object sender, ServerMessageEventArgs e)
        {
            MessageBox.Show(e.ToString().Replace("System.Data.SqlClient.SqlError:", ""), "Phục hồi thành công...", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void backup_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            lblPercent.Text = "Hoàn thành " + e.Percent.ToString() + " %";
            prbProgress.Value = Convert.ToInt32(e.Percent);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelectPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile1 = new OpenFileDialog();
            DialogResult Result = openfile1.ShowDialog();
            if (Result == DialogResult.OK)
            {
                txtPath.Text = openfile1.FileName;
            }
        }
    }
}
